package cn.gov.msa.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;

import cn.gov.msa.dao.MenuPermissionMapper;
import cn.gov.msa.model.MenuPermission;
import cn.gov.msa.service.MenuPermissionService;
import cn.gov.msa.util.DZZWCacheEvict;

public class MenuPermissionServiceImpl implements MenuPermissionService {
	
	@Autowired
	private MenuPermissionMapper menuPermissionMapper;
	
	@DZZWCacheEvict(value="dzzw_cache",key="#menuPermission[0].roleCode")
	@Override
	public boolean saveData(List<MenuPermission> menuPermission) {
		 List<Boolean> saves = new ArrayList<Boolean>();
		for (int i = 0; i < menuPermission.size(); i++) {
			if(menuPermissionMapper.insert(menuPermission.get(i))>0) {
				saves.add(true);
			}else {
				saves.add(false);
			}
			
		}
		if(saves.contains(false)) {
			return false;
		}else {
			return true;
		}
	}

	@Override
	public int deleteByRoleCode(String roleCode) {
		return menuPermissionMapper.deleteByRoleCode(roleCode);
	}

	@Override
	public List<MenuPermission> queryByRoleCode(String roleCode) {
		List<MenuPermission> menuPermissions = new ArrayList<MenuPermission>();
		menuPermissions = menuPermissionMapper.queryByRoleCode(roleCode);
		return menuPermissions;
	}

}
